package com.leon.alishop.core.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leon.alishop.core.cache.CacheManage;
import com.leon.alishop.core.cache.CacheTypeEnum;
import com.leon.alishop.core.enumeration.AttrInfoTypeEnum;
import com.leon.alishop.core.model.AttributesModel;
import com.leon.alishop.core.service.AttributesService;
import com.leon.alishop.dao.AttrInfoDao;
import com.leon.alishop.dao.AttrValInfoDao;
import com.leon.alishop.model.AttrInfo;
import com.leon.alishop.model.AttrValInfo;

@Service("attributesService")
public class AttributesServiceImpl implements AttributesService{

	@Override
	public List<AttributesModel> insertCrawlAttribute(List<AttributesModel> crawlAttributesList) {
		if(null == crawlAttributesList || crawlAttributesList.size()==0)
			return Collections.emptyList();
		AttrInfo ai;
		AttrValInfo avi;
		AttributesModel amt;
		boolean insert = false;
		for (AttributesModel a : crawlAttributesList) {
			if(a.getAttrType() == null)
				a.setAttrType(AttrInfoTypeEnum.NORMAL.ordinal());
			amt = this.findAttributeByNameAndType(a.getAttrName(), a.getAttrType());
			if(a.getId()==null){
				if(amt == null){
					ai = a.getAttrInfo();
					try {
//						attrInfoDao.insertSelective(ai);
						amt = insertAttributeWithFlushCache(ai);
						insert = true;
					} catch (Exception e) {
						throw new RuntimeException(e);
					}
					a.setId(ai.getId());
				}else{
					a.setId(amt.getId());
				}
			}
			if(null == a.getAttrValList())
				continue;
			for (AttrValInfo av : a.getAttrValList()) {
				av.setAttrId(a.getId());
				av.setValueType(a.getAttrType());
				if(av.getId()==null){
					avi = findAttrValInfoByNameAndAttrId(amt!=null ? amt.getAttrValList() : null, av.getValueName(), av.getAttrId());
					if(avi == null){
						try {
							avi = new AttrValInfo();
							BeanUtils.copyProperties(av, avi);
							attrValInfoDao.insertSelective(avi);
							amt.addAttrVal(avi);
							insert = true;
						} catch (Exception e) {
							throw new RuntimeException(e);
						}
						av.setId(avi.getId());
					}else{
						av.setId(avi.getId());
					}
				}
			}
		}
//		if(insert)
//			CacheManage.remove(AttributesService.ATTRIBUTES_CACHE_KEY, CacheTypeEnum.ATTRIBUTES);
		return crawlAttributesList;
	}
	
	public AttrValInfo findAttrValInfoByNameAndAttrId(List<AttrValInfo> list,String valName,Integer attrId){
		if(list != null){
			for (AttrValInfo av : list) {
				if(av.getValueName().equalsIgnoreCase(valName) 
						&& av.getAttrId().equals(attrId))
					return av;
			}
		}
		return null;
	}
	
	public List<AttributesModel> findAllAttributeList(){
		List<AttrInfo> attrs = attrInfoDao.findAllAttrInfo();
		List<AttrValInfo> attrVals =  attrValInfoDao.findAllAttrVal();
		if(attrs == null
				|| attrs.size()==0)
			return Collections.emptyList();
		Map<Integer,AttributesModel> attrMap = new HashMap<Integer,AttributesModel>();
		AttributesModel am;
		for (AttrInfo o : attrs) {
			am = new AttributesModel();
			try {
				BeanUtils.copyProperties(o, am);
			} catch (Exception e) {
				continue;
			}
			attrMap.put(am.getId(), am);
		}
		if(null != attrVals){
			for (AttrValInfo o : attrVals) {
				if(attrMap.containsKey(o.getAttrId())){
					attrMap.get(o.getAttrId()).addAttrVal(o);
				}
			}
		}
		return new ArrayList<AttributesModel>(attrMap.values());
	}
	
	public List<AttributesModel> findAllAttributeListWithCache(){
		List<AttributesModel> ams = (List<AttributesModel>) CacheManage.get(AttributesService.ATTRIBUTES_CACHE_KEY, CacheTypeEnum.ATTRIBUTES);
		if(ams == null){
			ams = this.findAllAttributeList();
			if(ams== null) {
				ams = Collections.emptyList();
			}
			CacheManage.put(AttributesService.ATTRIBUTES_CACHE_KEY, ams, CacheTypeEnum.ATTRIBUTES);
		}
		return ams;
	}
	
	public AttributesModel findAttributeByNameAndType(String name,Integer attrType){
		List<AttributesModel> ams = findAllAttributeListWithCache();
		if(ams != null){
			for (AttributesModel am : ams) {
				if(am.getAttrName().equalsIgnoreCase(name)
						&& am.getAttrType().equals(attrType))
					return am;
			}
		}
		return null;
	}
	
	@Override
	public AttributesModel findAttributeById(Integer attrId) {
		List<AttributesModel> ams = findAllAttributeListWithCache();
		if(ams != null){
			for (AttributesModel am : ams) {
				if(am.getId().equals(attrId))
					return am;
			}
		}
		return null;
	}
	
	public AttributesModel insertAttributeWithFlushCache(AttrInfo attrInfo){
		attrInfoDao.insertSelective(attrInfo);
		AttributesModel am = new AttributesModel();
		BeanUtils.copyProperties(attrInfo, am);
		List<AttributesModel> ams = findAllAttributeListWithCache();
		if(ams.isEmpty()){
			ams = new ArrayList<AttributesModel>();
			CacheManage.put(AttributesService.ATTRIBUTES_CACHE_KEY, ams, CacheTypeEnum.ATTRIBUTES);
		}
		ams.add(am);
		return am;
	}
	
	
	@Autowired
	private AttrInfoDao attrInfoDao;
	@Autowired
	private AttrValInfoDao attrValInfoDao;
	
	
	
}
